package com.app.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.biz.domain.request.BizFocusSchedulingProductProductionRequest;
import com.app.biz.domain.BizFocusSchedulingProductProduction;
import com.app.system.domain.BizIndustrialData;
import com.app.system.mapper.BizIndustrialDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizFocusSchedulingProductProductionFormalMapper;
import com.app.biz.domain.formal.BizFocusSchedulingProductProductionFormal;
import com.app.biz.service.IBizFocusSchedulingProductProductionFormalService;

/**
 * 重点调度产品产量正式SService业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@Service
public class BizFocusSchedulingProductProductionFormalServiceImpl implements IBizFocusSchedulingProductProductionFormalService
{
    @Autowired
    private BizFocusSchedulingProductProductionFormalMapper bizFocusSchedulingProductProductionFormalMapper;
    @Autowired
    private BizIndustrialDataMapper bizIndustrialDataMapper;
    /**
     * 查询重点调度产品产量正式S
     *
     * @param baseId 重点调度产品产量正式S主键
     * @return 重点调度产品产量正式S
     */
    @Override
    public BizFocusSchedulingProductProductionFormal selectBizFocusSchedulingProductProductionFormalByBaseId(String baseId)
    {
        return bizFocusSchedulingProductProductionFormalMapper.selectBizFocusSchedulingProductProductionFormalByBaseId(baseId);
    }

    /**
     * 查询重点调度产品产量正式S列表
     *
     * @param bizFocusSchedulingProductProductionFormal 重点调度产品产量正式S
     * @return 重点调度产品产量正式S
     */
    @Override
    public List<BizFocusSchedulingProductProductionFormal> selectBizFocusSchedulingProductProductionFormalList(BizFocusSchedulingProductProductionFormal bizFocusSchedulingProductProductionFormal)
    {
        return bizFocusSchedulingProductProductionFormalMapper.selectBizFocusSchedulingProductProductionFormalList(bizFocusSchedulingProductProductionFormal);
    }

    /**
     * 新增重点调度产品产量正式S
     *
     * @param bizFocusSchedulingProductProductionFormal 重点调度产品产量正式S
     * @return 结果
     */
    @Override
    public int insertBizFocusSchedulingProductProductionFormal(BizFocusSchedulingProductProductionFormal bizFocusSchedulingProductProductionFormal)
    {
        return bizFocusSchedulingProductProductionFormalMapper.insertBizFocusSchedulingProductProductionFormal(bizFocusSchedulingProductProductionFormal);
    }

    /**
     * 修改重点调度产品产量正式S
     *
     * @param bizFocusSchedulingProductProductionFormal 重点调度产品产量正式S
     * @return 结果
     */
    @Override
    public int updateBizFocusSchedulingProductProductionFormal(BizFocusSchedulingProductProductionFormal bizFocusSchedulingProductProductionFormal)
    {
        return bizFocusSchedulingProductProductionFormalMapper.updateBizFocusSchedulingProductProductionFormal(bizFocusSchedulingProductProductionFormal);
    }

    /**
     * 批量删除重点调度产品产量正式S
     *
     * @param baseIds 需要删除的重点调度产品产量正式S主键
     * @return 结果
     */
    @Override
    public int deleteBizFocusSchedulingProductProductionFormalByBaseIds(String[] baseIds)
    {
        return bizFocusSchedulingProductProductionFormalMapper.deleteBizFocusSchedulingProductProductionFormalByBaseIds(baseIds);
    }

    /**
     * 删除重点调度产品产量正式S信息
     *
     * @param baseId 重点调度产品产量正式S主键
     * @return 结果
     */
    @Override
    public int deleteBizFocusSchedulingProductProductionFormalByBaseId(String baseId)
    {
        return bizFocusSchedulingProductProductionFormalMapper.deleteBizFocusSchedulingProductProductionFormalByBaseId(baseId);
    }

    /**
     * 获取APP表格中的数据
     *
     * @param request
     * @return
     */
    @Override
    public List<BizFocusSchedulingProductProduction> getAppTableData(BizFocusSchedulingProductProductionRequest request) {
        String dates = request.getDates();
        DateTime parse = DateUtil.parse(dates, "yyyy年MM月");
        request.setDates(DateUtil.format(parse,"yyyy-MM-dd"));
        List<BizFocusSchedulingProductProduction> appTableData = bizFocusSchedulingProductProductionFormalMapper.getAppTableData(request);

//        List<BizIndustrialData> noParentNodeArgs = bizIndustrialDataMapper.selectBizIndustrialDataNoParentNode();
//        Map<String, List<BizIndustrialData>> collect = noParentNodeArgs.stream().collect(Collectors.groupingBy(BizIndustrialData::getParentId));
//
//        for(BizFocusSchedulingProductProduction item : appTableData){
//            String typeBaseId = item.getTypeBaseId();
//            if(collect.containsKey(typeBaseId)) {
//                List<BizIndustrialData> bizIndustrialData = collect.get(typeBaseId);
//                if(bizIndustrialData != null && !bizIndustrialData.isEmpty()){
//                    item.setHaveLeft("1");
//                }else{
//                    item.setHaveLeft("0");
//                }
//            }else{
//                item.setHaveLeft("0");
//            }
//        }
        return appTableData;
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizFocusSchedulingProductProductionFormalMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }
}
